package com.tanhua.server.controller;

import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 发布小视频
     *
     * @param picFile
     * @param videoFile
     * @return
     */
    @PostMapping
    public Object saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                            @RequestParam("videoFile") MultipartFile videoFile) {
        return this.videoService.saveVideo(picFile, videoFile);
    }

    /**
     * 查询小视频列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.videoService.queryVideoList(Math.max(page, 1), pageSize);
    }

    /**
     * @Author 张园
     * 视频点赞
     *
     * @param videoId
     * @return 返回的点赞数存储到响应体中
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeVideo(@PathVariable("id")String videoId){
        try {
            Long likeVideoCount=this.videoService.likeComment(videoId);
            if(likeVideoCount != null){
                return ResponseEntity.ok(likeVideoCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Author 张园
     * 视频取消点赞
     *
     * @param videoId
     * @return 返回的点赞数存储到响应体中
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> dislikeVideo(@PathVariable("id")String videoId){
        try {
            Long likeVideoCount=this.videoService.dislikeComment(videoId);
            if(likeVideoCount != null){
                return ResponseEntity.ok(likeVideoCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Author 张园
     * 评论列表
     *
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult>  queryCommentsList(@PathVariable("id")String videoId,
                                    @RequestParam(value = "page",defaultValue = "1")Integer page,
                                    @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult= this.videoService.queryCommentsList(videoId,page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Author 张园
     * 提交评论
     *
     * @param param comment评论内容
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> publishComments(@PathVariable("id")String videoId,
                                    @RequestBody Map<String,String>param){
        try {
            String content = param.get("comment");
            Boolean result=this.videoService.publishComments(videoId,content);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Author 张园
     * 评论点赞
     *
     * @param commentId 视频中的评论id
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String commentId) {
        try {
            Long likeCommentCount=this.videoService.likeComment(commentId);
            if(likeCommentCount != null){
                return ResponseEntity.ok(likeCommentCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * @Author 张园
     * 评论取消点赞
     *
     * @param commentId 视频中的评论id
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String commentId) {
        try {
            Long likeCommentCount=this.videoService.dislikeComment(commentId);
            if(likeCommentCount != null){
                return ResponseEntity.ok(likeCommentCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @Author 张园
     * 关注用户
     *
     * @param userId
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity<Void> followUser(@PathVariable("uid")Long userId){
        try {
            Boolean result= this.videoService.followUser(userId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @Author 张园
     * 取消关注用户
     *
     * @param userId
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity<Void> disFollowUser(@PathVariable("uid")Long userId){
        try {
            Boolean result= this.videoService.disFollowUser(userId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
